<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Artist_model extends CI_Model {
	
	function __construct() {
		parent::__construct();

	}

	/********************************************************************
	 * iMBC 프로그램 리스트
	 *******************************************************************/
	function select_program_list() {
		log_message('debug',"#### Artist_model::select_program_list()");

		$this->db->order_by('type','ASC');
		$this->db->order_by('priority','ASC');
		$query = $this->db->get('tb_imbc_program') or $this->logsql->log_sql('error');
		$result = $query->result_array();
		
		$this->logsql->log_sql();
		$this->logsql->log_result($result);
		
		return $result;
	}
	
		
	/********************************************************************
	 * iMBC 프로그램 추가
	 *******************************************************************/
	 	function insert_program($program) {
		log_message('debug',"#### Artist_model::insert_program($program)");

		$this->db->set('program_id',   $program['program_id']);
		$this->db->set('program_name', $program['program_name']);
		$this->db->set('type',         $program['type']);
		$this->db->set('priority',     $program['priority']);
		$this->db->set('story',        $program['story']);
		$this->db->set('start_dt',     $program['start_dt']);
		$this->db->set('end_dt',       $program['end_dt']);
		$this->db->set('photo_url',    $program['photo_url']);
		$this->db->set('use_yn',       'Y');
		$this->db->insert('tb_imbc_program');
	}
	
	
	/********************************************************************
	 * iMBC 프로그램 수정
	 *******************************************************************/
	 	function update_program($program) {
		log_message('debug',"#### Artist_model::update_program($program)");

		$this->db->set('program_id',   $program['program_id']);
		$this->db->set('program_name', $program['program_name']);
		$this->db->set('type',         $program['type']);
		$this->db->set('priority',     $program['priority']);
		$this->db->set('story',        $program['story']);
		$this->db->set('start_dt',     $program['start_dt']);
		$this->db->set('end_dt',       $program['end_dt']);
		$this->db->set('use_yn',       $program['use_yn']);
		$this->db->set('photo_url',    $program['photo_url']);
		$this->db->where('program_id', $program['program_id']);
		$this->db->update('tb_imbc_program');
		
		$this->logsql->log_sql();
	}
	

	/********************************************************************
	 * iMBC 프로그램 삭제
	 *******************************************************************/
	function delete_program($program_id) {
		log_message('debug',"#### Artist_model::delete_program($program_id)");
		
		$this->db->where('program_id',$program_id);
		$this->db->delete('tb_imbc_program') or $this->logsql->log_sql('error');
	}

	
	/********************************************************************
	 * iMBC 스타 리스트
	 *******************************************************************/
	function select_artist_list() {
		log_message('debug',"#### Artist_model::select_artist_list()");

		$sql  = PHP_EOL." SELECT DA.* ";
		$sql .= PHP_EOL."     , DP.position_name ";
		$sql .= PHP_EOL."     , P.program_name ";
		$sql .= PHP_EOL."     , P.type ";
		$sql .= PHP_EOL."     , A.artist_name ";
		$sql .= PHP_EOL."     , A.birth_yyyy ";
		$sql .= PHP_EOL."     , A.birth_mm ";
		$sql .= PHP_EOL."     , A.birth_dd ";
		$sql .= PHP_EOL."     , A.sex ";
		$sql .= PHP_EOL."     , A.job ";
		$sql .= PHP_EOL."     , A.photo profile_photo ";
		$sql .= PHP_EOL." FROM  tb_imbc_display_artist DA ";
		$sql .= PHP_EOL." INNER JOIN tb_imbc_program P ON P.program_id = DA.program_id ";
		$sql .= PHP_EOL." INNER JOIN tb_imbc_display_position DP ON DP.position_id = DA.position_id ";
		$sql .= PHP_EOL." INNER JOIN tb_artist A ON A.idx = DA.artist_idx ";
//		$sql .= PHP_EOL." WHERE DA.use_yn =  'Y' ";
		$sql .= PHP_EOL." ORDER BY DP.position_name ASC, P.program_name ASC, DA.priority ASC ";
		$query = $this->db->query($sql) or $this->logsql->log_sql('error');
		$result = $query->result_array();
		
		$this->logsql->log_sql();
		$this->logsql->log_result($result);
		
		return $result;
	}
	

	/********************************************************************
	 * iMBC 분류(보여줄곳) 리스트
	 *******************************************************************/
	function select_display_position_list() {
		log_message('debug',"#### Artist_model::select_display_position_list()");

		$query = $this->db->get('tb_imbc_display_position') or $this->logsql->log_sql('error');
		$result = $query->result_array();
		
		$this->logsql->log_sql();
		$this->logsql->log_result($result);
		
		return $result;
	}
	

	/********************************************************************
	 * iMBC 스타 리스트 검색 (키워드)
	 *******************************************************************/
	function select_artist_search_list($search_keyword) {
		log_message('debug',"#### Artist_model::select_artist_search_list($search_keyword)");

		$this->db->select('idx, artist_name, job, birth_yyyy, birth_mm, birth_dd, sex, body, photo');
		$this->db->like('keyword', $search_keyword);
		$query = $this->db->get('tb_artist') or $this->logsql->log_sql('error');
		$result = $query->result_array();
		
		$this->logsql->log_sql();
		$this->logsql->log_result($result);

		return $result;
	}
	

	/********************************************************************
	 * iMBC 스타 추가
	 *******************************************************************/
	 	function insert_artist($artist) {
		log_message('debug',"#### Artist_model::insert_artist($artist)");

		$this->db->set('position_id', $artist['position_id']);
		$this->db->set('program_id',  $artist['program_id']);
		$this->db->set('artist_idx',  $artist['artist_idx']);
		$this->db->set('priority',    $artist['priority']);
		$this->db->set('role',        $artist['role']);
		$this->db->set('photo_url',   $artist['photo_url']);
		$this->db->set('use_yn',      'Y');
		$this->db->insert('tb_imbc_display_artist') or $this->logsql->log_sql('error');
	}
	

	/********************************************************************
	 * iMBC 스타 수정
	 *******************************************************************/
	 	function update_artist($artist) {
		log_message('debug',"#### Artist_model::update_artist($artist)");

		$this->db->set('position_id', $artist['position_id']);
		$this->db->set('program_id' , $artist['program_id']);
		$this->db->set('artist_idx' , $artist['artist_idx']);
		$this->db->set('priority'   , $artist['priority']);
		$this->db->set('role'       , $artist['role']);
		$this->db->set('photo_url'  , $artist['photo_url']);
		$this->db->set('use_yn'     , $artist['use_yn']);
		$this->db->where('position_id', $artist['position_id']);
		$this->db->where('program_id' , $artist['program_id']);
		$this->db->where('artist_idx' , $artist['artist_idx']);
		$this->db->update('tb_imbc_display_artist') or $this->logsql->log_sql('error');
	}
	
	
	/********************************************************************
	 * iMBC 스타 삭제
	 *******************************************************************/
	function delete_artist($position_id, $program_id, $artist_idx) {
		log_message('debug',"#### Artist_model::delete_artist($position_id, $program_id, $artist_idx)");
		
		$this->db->where('position_id',$position_id);
		$this->db->where('program_id',$program_id);
		$this->db->where('artist_idx',$artist_idx);
		$this->db->delete('tb_imbc_display_artist') or $this->logsql->log_sql('error');
	}

}

/* End of file artist_model.php */
/* Location: ./application/models/imbc/artist_model.php */